import Vue from 'vue';
import VueRouter from 'vue-router';
import store from 'store/index';

/**
 * 系统根级别路由 - 一级路由
 */

import messageRouter from '../views/message/router/index.js';
import profileRouter from '../views/profile/router/index.js';
import orderRouter from '../views/order/router/index.js';

Vue.use(VueRouter);

// 异步引入
const myOrder = () => import('../views/order/order.vue');
const myMessageList = () => import('../views/message/message.vue');
const homePage = () => import('../views/home/home.vue');
const profile = () => import('../views/profile/profile.vue');
const login = () => import('../views/login/login.vue');
const register = () => import('../views/register/register.vue');
const errorPage = () => import('../views/error/index.vue');
const forgetPassword = () => import('../views/forget-password/forget-password.vue');
const transactionDetail = () => import('../views/detail/transaction-details/transaction-details.vue');

const routes = [
    {
        path: '/index',
        redirect: { name: 'index' }
    },
    {
        path: '/',
        component: homePage,
        name: 'index',
        meta: {
            keepAlive: true,
            title: '首页',
            hideLeftArrow: true
        }
    },
    {
        path: '/message',
        name: 'message',
        component: myMessageList,
        children: messageRouter,
        meta: {
            title: '消息',
            hideLeftArrow: true
        }
    },
    {
        path: '/orders',
        name: 'orders',
        component: myOrder,
        children: orderRouter,
        meta: {
            title: '订单',
            hideLeftArrow: true
        }
    },
    {
        path: '/profile',
        name: 'profile',
        component: profile,
        children: profileRouter,
        meta: {
            title: '我的',
            hideLeftArrow: true
        }
    },
    {
        path: '/login',
        name: 'login',
        component: login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/register',
        name: 'register',
        component: register,
        meta: {
            title: '注册'
        }
    },
    {
        path: '/forget',
        name: 'forgetPassword',
        component: forgetPassword,
        meta: {
            title: '重置密码'
        }
    },
    {
        path: '/detail/:id',
        name: 'detail',
        component: () => import('views/detail/detail'),
        children: [
            {
                path: 'transaction-detail',
                name: 'transactionDetail',
                component: transactionDetail,
                meta: {
                    title: '接单详情'
                }
            }
        ],
        meta: {
            title: '详情'
        }
    },
    {
        path: '*',
        name: 'error',
        component: errorPage,
        meta: {
            title: '未知访问'
        }
    }
];

const router = new VueRouter({
    mode: 'hash',
    base: process.env.BASE_URL,
    routes,
    linkActiveClass: 'currentRoute'
});

// 全局导航守卫
router.beforeEach((to, from, next) => {
    store.commit('simpleAssignment', {
        state: 'title',
        value: to.meta.title
    });
    next();
});

export default router;
